//
//  CNGameSetViewController.m
//  chuiniu
//
//  Created by zheng ji on 14-10-9.
//  Copyright (c) 2014年 jxw. All rights reserved.
//

#import "CNGameSetViewController.h"
#import "CNAppDelegate.h"
#import "common/constants.h"
#import "CNGameUtil.h"
#import "CNPlayerLevelViewController.h"
#import "CNBuyGoldCoinController.h"
#import "CNViewController.h"
@interface CNGameSetViewController ()
{
    UITableView* _tablePlayer;
    CNAppDelegate* _appDelegate;
    NSUserDefaults* _userDefaults;
    NSString* _winCount;
    NSString* _faitureCount;
    
    CNGameClient* _gameClient;

    iToast *toast;
    CNPlayer* _currentPlayer;
     CNPlayer* tempVSPlayer;
}
@end

@implementation CNGameSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _appDelegate=(CNAppDelegate*)[UIApplication sharedApplication].delegate;
     _userDefaults=[NSUserDefaults standardUserDefaults];
    
    
   
    
   
    _winCount=[_userDefaults objectForKey:@"PlayerWinCount"];
      _faitureCount=[_userDefaults objectForKey:@"PlayerFailtureCount"];
  
    if(_winCount==nil || [_winCount isEqualToString:@""]) _winCount=@"0";
    if(_faitureCount==nil || [_faitureCount isEqualToString:@""]) _faitureCount=@"0";
    
    
    
    _currentPlayer=[CNGameUtil getCurrentPlayerFromUserDefaults];
    [self initTable];
    _appDelegate=(CNAppDelegate*)[UIApplication sharedApplication].delegate;
    _gameClient=_appDelegate.gameClient;
    _gameClient.gameMainViewController=self;
    
    
}
-(void)viewDidAppear:(BOOL)animated{
    _currentPlayer=[CNGameUtil getCurrentPlayerFromUserDefaults];
    [_tablePlayer reloadData];
}
-(void)initTable{
 CGRect frame=   self.view.frame;
    _tablePlayer=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 250) style:UITableViewStylePlain];
    _tablePlayer.delegate=self;
    _tablePlayer.dataSource=self;
    
    
    [_tablePlayer registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse_themeCell"];
    [self.view addSubview:_tablePlayer];
    
    
    UIView* viewPlayerGuess=[[UIView alloc]initWithFrame:CGRectMake(frame.origin.x+50, 255, frame.size.width-100, 25)];
    viewPlayerGuess.layer.cornerRadius=5.5;
    viewPlayerGuess.layer.borderWidth=1;
    viewPlayerGuess.backgroundColor=RGB(0,255,0);//RGB(255,69,0);
    viewPlayerGuess.layer.borderColor=RGB(0,255,0).CGColor;
    
    UILabel* lblConfirm=[[UILabel alloc]initWithFrame: CGRectMake(2,2, frame.size.width-100, 20)];
    lblConfirm.font=[UIFont systemFontOfSize:20];
    lblConfirm.textColor=[UIColor whiteColor];
    lblConfirm.text=@"购买金币";
    lblConfirm.textAlignment=NSTextAlignmentCenter;
    lblConfirm.userInteractionEnabled=YES;
    [viewPlayerGuess addSubview:lblConfirm];
    [self.view addSubview:viewPlayerGuess];
    
    UITapGestureRecognizer* tapGeustureConfirm=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buyGoldCoin:)];
    tapGeustureConfirm.numberOfTapsRequired=1;
    tapGeustureConfirm.numberOfTouchesRequired=1;
    
    [lblConfirm addGestureRecognizer:tapGeustureConfirm];
    
}
-(void)buyGoldCoin:(UIGestureRecognizer*) gesture{
    
    if(!_appDelegate.gameClient.isConnected){
        
        UIAlertView* alertView=[[UIAlertView alloc]initWithTitle:@"" message:@"未连接服务器,不能购买金币" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alertView show];
        alertView=nil;
        [_appDelegate.gameClient connectToServer];
        return ;
        
    }
    if([SKPaymentQueue canMakePayments]){
        
    
    CNBuyGoldCoinController* buyController=[self.storyboard instantiateViewControllerWithIdentifier:@"CNBuyGoldCoinController"];
    
    

    // [buyController loadProducts];
      [self.navigationController pushViewController:buyController animated:YES];
    }else{
        
        UIAlertView* alertView=[[UIAlertView alloc]initWithTitle:@"" message:@"请再【设置】中开启应用内置购买" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alertView show];
        alertView=nil;
    }
    //NSLog(@"dd");
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; {
    return 1;
}
-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"reuse_themeCell";
    
   // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
   UITableViewCell* cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]   ;
    
   /* if(cell ==nil){
        cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]   ;
  
    }*/
    
    
    if(_currentPlayer!=nil){
       
        if(indexPath.row==0){
            cell.textLabel.text=@"昵称";
            cell.detailTextLabel.text=_currentPlayer.playerName;
        }
        if(indexPath.row==1){
            cell.textLabel.text=@"等级";
            cell.detailTextLabel.text=_currentPlayer.levelName;
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

        }
        
        if(indexPath.row==2){
            cell.textLabel.text=@"金币";
            cell.detailTextLabel.text=_currentPlayer.coins;
        }
        if(indexPath.row==3){
            cell.textLabel.text=@"胜负";
            cell.detailTextLabel.text=[NSString stringWithFormat:@"胜:%@,负:%@",_winCount,_faitureCount];
        }
    }
   
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 
    if(indexPath.row==1){
        
        CNPlayerLevelViewController* playerLevelController=(CNPlayerLevelViewController*)[ self.storyboard instantiateViewControllerWithIdentifier:@"CNPlayerLevelViewController"];
        [self.navigationController pushViewController:playerLevelController animated:YES];

    }
}
-(void)toastTimeOver:(int)toastTag{
    
}

/**
 有玩家邀请对战
 */
-(void) requestPK:(CNPlayer*)vsPlayer{
    
    tempVSPlayer=vsPlayer;
    UIAlertView* alertView=[[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"玩家[%@]想和您对战，是否接受?",vsPlayer.playerName] delegate: self cancelButtonTitle:@"接受" otherButtonTitles:@"残忍拒绝", nil];
    // alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
    alertView.tag=tagAlertViewRequestPK;
    [alertView show];
    alertView=nil;
    
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
 if([alertView tag]==tagAlertViewRequestPK){
        if(  buttonIndex == 0) {
            
            //接受请求对战
            [self startMutilPlay];
        }else{
            //拒绝对战请求
            [_appDelegate.gameClient sendRejectVSCommand:tempVSPlayer];
        }
    }
}
//开启对战游戏
-(void)startMutilPlay{
   
    
    //发送接收对战消息
    
    [_appDelegate.gameClient sendAcceptVSCommand:_currentPlayer vsPlayerID:tempVSPlayer];
    
    CNViewController* viewCtrl=(CNViewController*)[ self.storyboard instantiateViewControllerWithIdentifier:@"CNViewController"];
    
    viewCtrl.gameModel=GameModelMutilPlayer;
    //viewCtrl.gameStatus=GameStatusNormal;
    _appDelegate.vsPlayer=tempVSPlayer;
    _appDelegate.gamePlayModel=GameModelMutilPlayer;
    _appDelegate.gameClient.gameMainViewController=viewCtrl;
    _appDelegate.gameStatus=GameStatusPlaying;
    [self presentViewController:viewCtrl animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
